package liangtWorkSpace.threadDemo.exe;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author liangt
 * @ClassName: CountDown
 * @title: CountDown
 * @projectName liangtWorkSpace.threadDemo.exe
 * @description: 模拟多用户
 * @date 2020/10/30 19:10
 */
public class CountDown {
    //用户数量
    static int user = 50000;
    //总下载数量
    static int down = 50000;
    //下载次数
    static int count = 0;

    public static void main(String[] args) {
        //调度器
        ExecutorService executorService = Executors.newCachedThreadPool();

        //调用者
        final Semaphore semaphore = new Semaphore(user);

        //模拟调度器
        for (int i = 0 ; i < down ; i++){
            executorService.execute(()->{
                try {
                    semaphore.acquire();
                    add();
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(count);
    }

    public static void add(){
        count++;
    }
}
